/**
 *
 *
 * 
 *
 * 
 */

package io.dbw.modules.business.oauth2;

import com.alibaba.fastjson.JSON;
import io.dbw.common.enums.ResultStatusEnum;
import io.dbw.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * oauth2过滤器
 *
 * 
 */
@Slf4j
public class OAuth2Filter extends AuthenticatingFilter {

    @Override
    protected AuthenticationToken createToken(ServletRequest request, ServletResponse response) throws Exception {
        //获取请求token
        String token = getRequestToken((HttpServletRequest) request);

        if(StringUtils.isBlank(token)){
            return null;
        }

        return new OAuth2Token(token);
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        if(((HttpServletRequest) request).getMethod().equals(RequestMethod.OPTIONS.name())){
            return true;
        }

        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        //获取请求token，如果token不存在，直接返回401
        String token = getRequestToken((HttpServletRequest) request);
        if(StringUtils.isBlank(token)){
            HttpServletResponse httpResponse = (HttpServletResponse) response;
            log.info("onAccessDenied--->token已过期");
            httpResponse.setCharacterEncoding("utf-8");
            httpResponse.setHeader("Content-Type", "text/html; charset=utf-8");
            httpResponse.setContentType("text/html; charset=utf-8");
            PrintWriter writer = httpResponse.getWriter();
            String result= JSON.toJSONString(Result.info(ResultStatusEnum.USER_OVERDUE)) ;
            writer.write(result.toCharArray());
            writer.close();
            return false;
        }

        return executeLogin(request, response);
    }

    @Override
    protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        log.info("onLoginFailure--->token已过期");
        httpResponse.setCharacterEncoding("utf-8");
        httpResponse.setHeader("Content-Type", "text/html; charset=utf-8");
        httpResponse.setContentType("text/html; charset=utf-8");
        try {
            PrintWriter writer = httpResponse.getWriter();
            String result= JSON.toJSONString(Result.info(ResultStatusEnum.USER_OVERDUE)) ;
            writer.write(result.toCharArray());
            writer.close();
        } catch (IOException e1) {
           log.info("onLoginFailure is error:",e);
        }
        return false;
    }

    /**
     * 获取请求的token
     */
    private String getRequestToken(HttpServletRequest httpRequest){
        //从header中获取token
        String token = httpRequest.getHeader("token");
        return token;
    }


}
